package ex55.mm.flow.impound.quote.admin;

import com.google.common.collect.Maps;
import ex55.mm.flow.impound.quote.AggregateDataFacade;
import ex55.mm.flow.impound.quote.aggregate.ExchangeProduct;
import ex55.mm.flow.impound.quote.exchange.api.ExchangeType;
import ex55.mm.flow.impound.quote.exchange.api.MarketDataDepth;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/v1/admin")
public class ImpoundAdminApi {

    @Autowired
    private AggregateDataFacade aggregateDataFacade;

    @RequestMapping("/listExchangeProduct")
    public Map<String, List<ExchangeProduct>> listExchangeProduct(
            @RequestParam("token") String token,
            @RequestParam(name = "exchangeType", required = false) ExchangeType exchangeType,
            @RequestParam(name = "symbol", required = false) String symbol) {
        validateToken(token);
        return aggregateDataFacade.listExchangeProduct(exchangeType, symbol);
    }

    @RequestMapping("/updateWeight")
    public Map<String, Object> updateWeight(
            @RequestParam("token") String token,
            @RequestParam("accountId") String accountId,
            @RequestParam("productId") String productId,
            @RequestParam("weight") BigDecimal weight) {
        validateToken(token);

        if (Strings.isEmpty(productId)) {
            throw new IllegalArgumentException("productId empty");
        }

        aggregateDataFacade.updateWeight(productId, accountId, "update weight", weight);

        Map<String, Object> response = Maps.newHashMap();

        response.put("status", "success");
        response.put("productId", productId);
        response.put("weight", weight);

        return response;
    }

    private void validateToken(String token) {
        if (Strings.isEmpty(token) || !"AD214ABA5DA6684576A8756BF3366B99".equals(token)) {
            throw new IllegalArgumentException("token error");
        }
    }

    @RequestMapping("/book")
    public MarketDataDepth getMarketDataDepth(String productId) {
        if (Strings.isEmpty(productId)) {
            throw new IllegalArgumentException("productId error");
        }
        return aggregateDataFacade.getData_MarketDataDepth(productId);
    }
}
